package com.novabluetooth.services.servicedevices;

import com.novabluetooth.services.TJBluetoothLeService;

import android.bluetooth.BluetoothGattService;
import android.util.Log;

/**
 * 血氧nova
 * 
 * @author nova
 *
 */
public class XueYangNovaService extends TJBluetoothLeService {
	/**
	 * 三诺安稳型
	 */

	private String BT_readData = "7d81a1000000000000";
	private String BT_closeDevice = "7d81a2000000000000";
	@Override
	public void servicesDiscovered() {
		super.servicesDiscovered();
		BluetoothGattService gattService = mBluetoothGatt.getService(serviceUUID);
		readGattCharact = gattService.getCharacteristic(readCharactUUID);
		writeGattCharact = gattService.getCharacteristic(writeCharactUUID);
		mBluetoothGatt.setCharacteristicNotification(readGattCharact, true);
		if (writeGattCharact == null) {
			System.out.println("writeGattCharact为空");
		}
			ifSuccess = writeDataToDevice(BT_readData);
	}

	@Override
	public boolean isRightData(byte[] buffer) {
		super.isRightData(buffer);
		// 血氧
		int readSize = buffer.length;

		if (readSize <= 0) {
			Log.e("LeeTag", "数据为空");
			ifSuccess = false;
		} else if (readSize < 9) {
			Log.e("LeeTag", "BTThread Data is Short");
			ifSuccess = false;
		} else if (((buffer[0] & 0xff) != 0x01)) {// 1
			Log.e("LeeTag", "BTThread Error Data Packet Header" + buffer[0]);
			ifSuccess = false;
		} else if ((buffer[6] & 0xff & 0x7F) == 0) {

			Log.e("LeeTag", "Error 6byte:" + (buffer[6] & 0xff & 0x7F));
			ifSuccess = false;
		} else if ((buffer[5] & 0xff & 0x7F) == 0) {

			Log.e("LeeTag", "Error 5byte:" + (buffer[5] & 0xff & 0x7F));
			ifSuccess = false;
		} else {
			// 血氧饱和度
			int baohedu = (buffer[6] & 0x7F & 0xFF);
			// 脉率
			int mailv = (buffer[5] & 0x7F & 0xFF);

			// checkData = "血氧饱和度：" + baohedu + " 心率：" + mailv;
			checkData = String.valueOf(baohedu) + ";" + String.valueOf(mailv);
			ifSuccess = true;
		}
		return ifSuccess;

	}

	@Override
	public void closeDevice() {
		super.closeDevice();
		ifSuccess = writeDataToDevice(BT_closeDevice);
	}

}
